var qyxz_app = function() {//企业管理-新增或修改
	var path = Constants.amUrlPath; 
	var qyid = manager.findParamFromUrl('qyid');
	var ractive;
	var qyxzApp ={
		URL:{
			list: function(){//返回列表html页面
				return "qygl.html";
			},
			save: function(){//企业新增路径post
				return path + "qygl/insertQyxx";
			},
			update: function(){//企业修改路径post
				return path + "qygl/updateQyxx?t="+ new Date().getTime();
			},
			getQyxxById: function(id){//禁用get
				return path + "qygl/getQyxxById?qyid="+id;
			}
		},
		validateStr: function(str,id,errText){//非空验证
	    	 if ($.trim(str) == ''){
	    		 document.getElementById(id).innerHTML = errText;
	    		 document.getElementById(id).style.display = "inline-block";
	             return false;
	         } else {
	        	 document.getElementById(id).innerHTML = '';
	        	 document.getElementById(id).style.display = "none";
	             return true;
	         }
	    },
		edit: function(param,qyid){
			//先隐藏旧的错误信息
			qyxzApp.validateStr('str','zzjgdmerr','');
			qyxzApp.validateStr('str','qymcerr','');
			qyxzApp.validateStr('str','qyymerr','');
			//判断条件
			if(!qyxzApp.validateStr(param.zzjgdm,'zzjgdmerr',"Enter the organization's 9-digit code")){return}
			if(param.zzjgdm.length != 9){
				qyxzApp.validateStr('','zzjgdmerr',"Enter the organization's 9-digit code");
				return
			}
			if(!qyxzApp.validateStr(param.qymc,'qymcerr','Enter the enterprise name')){return}
			if(!qyxzApp.validateStr(param.qyym,'qyymerr','Enter enterprise domain name')){return}
			//执行
			if(qyid!=""){
				param.id = qyid;
				confirm({
					txt: "Are you sure to modify the enterprise?",
					successCallBackFun: function(){
						qyxzApp.update(param)
					},
					cancelCallBackFun: function(){
					}
				});	
				
			}else{
				//新增：业务域
				param.ywy = Constants.YWY.WD_CODE;
				confirm({
					txt: "Confirm new enterprise?",
					successCallBackFun: function(){
						qyxzApp.save(param)
					},
					cancelCallBackFun: function(){
					}
				});	
			}
		},
		save: function(param){
			$.ajax({
				type: "post",
				url: qyxzApp.URL.save(),
				data: JSON.stringify(param),
				contentType: "application/json",
				success: function(result){
					if(!result.result){//失败提示
						/*if(result.code == "E10001"){qyxzApp.validateStr('','zzjgdmerr','组织机构代码重复！');return}
						if(result.code == "E10002"){qyxzApp.validateStr('','qymcerr','企业名称重复！');return}
						if(result.code == "E10003"){qyxzApp.validateStr('','qyymerr','企业域名重复！');return}
						alert({txt: "保存失败！",callBackFun: callBackSuccessFun});					
						function callBackSuccessFun(){}*/
						manager.errorMsg(result);
					}else{//失败提示
						var objs = {txt:"Saved successfully!",callBackFun:function(){
							window.location.href = qyxzApp.URL.list()
						}}
						success(objs);
					}
				}
			})
		},
		update: function(param){
			$.ajax({
				type: "post",
				url: qyxzApp.URL.update(),
				data: JSON.stringify(param),
				contentType: "application/json",
				success: function(result){
					if(!result.result){//失败提示
						/*if(result.code == "E10001"){qyxzApp.validateStr('','zzjgdmerr','组织机构代码重复！');return}
						if(result.code == "E10002"){qyxzApp.validateStr('','qymcerr','企业名称重复！');return}
						if(result.code == "E10003"){qyxzApp.validateStr('','qyymerr','企业域名重复！');return}
						alert({txt: "保存失败！",callBackFun: callBackSuccessFun});					
						function callBackSuccessFun(){}*/
						manager.errorMsg(result);
					}else{//成功提示
						var objs = {txt:"Saved successfully!",callBackFun:function(){
							window.location.href = qyxzApp.URL.list()
						}}
						success(objs);
					}
				}
			})
		},
		showQyxx: function(qyid,ractive){
			$.ajax({
				type: "get",
				url: qyxzApp.URL.getQyxxById(qyid),				
				async: false,
				success: function(data) {//数据绑定
					//console.log(data);
					ractive.set('zzjgdm',data.zzjgdm);
					ractive.set('qymc',data.qymc);	
					ractive.set("qyym",data.qyym);
				}
			})
		}
	}
	//----------------------------------------------------------------------------start
	//logo上传TODO
	
	//banner上传TODO
	
	//----------------------------------------------------------------------------end
	function renderView() {//渲染页面
		ractive = new Ractive({
			el: ".hp-container",
			template: "#qyxz-template",
			onrender: function() {   
				//...
			},
			oncomplete: function() {
				if(qyid != ""){//显示默认数据
					$("#qyzzjgdm").attr("disabled","true");
					qyxzApp.showQyxx(qyid,ractive);
					$(".hp-dqye span").html("Modify the enterprise");
				}
				qyxzApp.validateStr(ractive.get('zzjgdm'),'zzjgdmerr',"Enter the organization's 9-digit code");
			}
		});
		
		ractive.on({
			save: function() {//保存
				var param = {
					zzjgdm: $.trim(ractive.get('zzjgdm')),
					qymc: $.trim(ractive.get('qymc')),
					qyym: $.trim(ractive.get('qyym')),
					logodz:"",
					bdz:"",
					mbbh:""
				}
				//去空格
				ractive.set('zzjgdm',$.trim(ractive.get('zzjgdm')));
				ractive.set('qymc',$.trim(ractive.get('qymc')));
				ractive.set('qyym',$.trim(ractive.get('qyym')));
				qyxzApp.edit(param,qyid);
			},
			cancel: function(){//取消
				window.location.href = qyxzApp.URL.list();
			},
			zzjgdm: function(e){//组织机构代码格式修正
				var currV = $(e.node).val();
				currV = currV.replace(/[\u4e00-\u9fa5]/g,'');
				ractive.set("zzjgdm", currV);
			},
			qyym: function(e){
				var currV = $(e.node).val();
				currV = currV.replace(/[^(A-Za-z0-9)]/g,'');
				ractive.set("qyym", currV);
			}
		});
	}
	
	return {
		init : function() {
			renderView();
		}
	};
}();
$(document).ready(qyxz_app.init);
